package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author gxm
 * @since 2023-12-17
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor apAuthor) {
        //判断是否已经是文章作者
        LambdaQueryWrapper<ApAuthor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApAuthor::getUserId, apAuthor.getUserId());
        ApAuthor one = this.getOne(wrapper);
        if (one == null) {
            //如果不是,保存返回
            apAuthor.setCreatedTime(new Date());
            this.save(apAuthor);
            return ResponseResult.okResult(apAuthor);
        } else {
            //如果是,直接返回
            return ResponseResult.okResult(one);
        }
    }
}
